package rsj.service.export.org.test;

import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rsj.api.org.entity.UsageReportParam;
import rsj.api.org.importdata.ImportEasyExcelInvoke;
import rsj.service.export.org.IOrgDownloadDetail;
import rsj.service.usage.IUsageTemplateMethod;
import rsj.sql.org.OrgProduceDetailDao;

/**
 * 机构管理-测试环境下载详情
 *
 * @author yuanqinglong
 * @since 2021/3/5 14:00
 */
@Component
public class OrgDownloadTestDetail implements IOrgDownloadDetail {


    @Autowired
    private OrgProduceDetailDao orgProduceDetailDao;


    @Override
    public TableNode queryTableByPage(UsageReportParam usageReportParam) {
        return orgProduceDetailDao.downloadTestDetail(usageReportParam);
    }

    /**
     * 生成查询测试环境使用详情语句
     *
     * @param usageReportParam 参数
     * @return sql
     */
    @Override
    public String generateQueryStatement(UsageReportParam usageReportParam) {
        return " SELECT log_ip,log_reqtime, log_reqsernumber,stat_env,CASE log_iscost WHEN '1' THEN '是' ELSE '否' END log_iscost, " +
                "        stat_org_sname, log_orgp_id,log_prod_uid, stat_prod_name FROM sys.oper_test_log test  LEFT JOIN ( " +
                "           SELECT stat_orgp_id,stat_org_sname,regexp_split_to_table(stat_prod_name,',') AS stat_prod_name,stat_env,regexp_split_to_table(stat_prod_uid,',') AS split_prod_uid FROM sys.sys_stat " +
                "     ) split ON split_prod_uid = test.log_prod_uid AND test.log_orgp_id = split.stat_orgp_id " +
                "            LEFT JOIN sys.sys_authon AS au ON au.aut_keyid = log_user_id LEFT JOIN sys.sys_org ON org_id = aut_org_id" +
                " WHERE log_orgp_id = #{statOrgpId} AND aut_org_id = #{statOrgId}" +
                " AND stat_env = '" + IUsageTemplateMethod.TEST_ENVIRONMENT + "' AND log_iscost = '" + ImportEasyExcelInvoke.IS_COST + "'";
    }


    /**
     * 生成导出的文件名称
     *
     * @param usageReportParam usageReportParam参数
     * @return 文件名
     */
    @Override
    public String generateExportFileName(UsageReportParam usageReportParam) {
        return this.queryOrgNameById(usageReportParam.getStatOrgId()) + "-测试环境-使用详情.xlsx";
    }


}
